﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using FluentValidation;
using FluentValidation.Attributes;
using PicStore.Common;
using PicStore.Common.Ioc;
using PicStore.BLL.Interface;

namespace PicStore.Web.Models.Category
{
    [Serializable]
    [Validator(typeof(CategoryModelValidator))]
    public class CategoryModel
    {
        [Display(Name = "Category Name")]
        public string CategoryName { get; set; }

        [Display(Name = "Item Name")]
        public string ItemName { get; set; }

        [Display(Name = "Item Value")]
        public string ItemValue { get; set; }

        [Display(Name = "Description")]
        public string Description { get; set; }

        [Display(Name = "Creater Name")]
        public string CreaterName { get; set; }

        public int Id { get; set; }
        public string CreateDate { get; set; }

        [Display(Name = "Category")]
        public int CategoryId { get; set; }

        public List<System.Web.Mvc.SelectListItem> CategoryList { get; set; }

        public bool IsValid { get; set; }
        public string ErrorMessage { get; set; }

        public static CategoryModel BuildCategoryModel(Model.CategoryInfo category)
        {
            var model = new CategoryModel()
            {
                Id = category.Id,
                CategoryName = category.CategoryName,
                CreateDate = category.CreateTime.ToShortDateString(),
                ItemName = category.ItemName,
                ItemValue = category.ItemValue,
                Description = category.Description,
                CategoryId = category.CategoryId,
                CreaterName = category.CreaterName,
            };

            return model;
        }

        public static Model.CategoryInfo BuildUserByUserModel(CategoryModel category)
        {
            var model = new Model.CategoryInfo()
            {
                Id = category.Id,
                ItemName = category.ItemName,
                CreateTime = DateTime.Parse(category.CreateDate),
                CategoryName = category.CategoryName,
                CategoryId = category.CategoryId,
                Description = category.Description,
                CreaterName = category.CreaterName,
                ItemValue = category.ItemValue,
            };

            return model;
        }
    }

    public class CategoryModelValidator : AbstractValidator<CategoryModel>
    {
        public CategoryModelValidator()
        {
            RuleFor(x => x.ItemName).NotNull().WithMessage(string.Format(Constants.MustInput, "Category Item Name"));
            RuleFor(x => x.ItemValue).NotNull().WithMessage(string.Format(Constants.MustInput, "Category Item Value"));
            RuleFor(x => x.ItemValue).Must((form, x) => IsItemValueNotDuplicated(form.CategoryId, x)).WithMessage(string.Format(Constants.DuplicatedError, "Category Item Value"));
        }

        private bool IsItemValueNotDuplicated(int categoryId, string itemValue)
        {
            var service = ObjectFactory.GetInstance<ICategoryService>();
            var list = service.GetItemValuesByCategoryId(categoryId);
            return list.Contains(itemValue) == false;
        }
    }
}